$(function () {
    /*ifram 自适应高度*/
    $(".flexiframe").each(function (index) {
        var that = $(this);
        (function () {
            setInterval(function () {
                setIframeHeight(that[0])
            }, 200)
        })(that)
    });
    getskincookie();
    getsitecookie();
    $('#side-menu').metisMenu();
    urlchange();
    /*换肤*/
    $("#ticky-skin ul li a").click(function () {
        var v = $(this).attr("data-val");
        setcookie("tickyskin", v);
        saveUserdata("tickyskin", v);
        var hrefStr = $("#skin").attr("href");
        var hrefRes = hrefStr.substring(0, hrefStr.lastIndexOf('skin/')) + 'skin/' + v + '/skin.css';
        $(window.frames.document).contents().find("#skin").attr("href", hrefRes);
    });
    /*切换站点*/
    $("#ticky-site ul li a").click(function () {
        var v = $(this).attr("data-val");
        var url = $(this).attr("data-url");
        setcookie("tickysite", v);
        saveUserdata("tickysite", v);
        $('#site_domain').attr('href', url);
        var title = $(this).html() + '<i class="fa fa-caret-down"></i>';
        $('#ticky-skin-site').html(title);
    })
    langchange();
});

function setIframeHeight(iframe) {
// 使用前先将子页面文档声明改为
//<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    if (iframe) {
        var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
        if (iframeWin.document.body) {
            iframe.height = document.documentElement.clientHeight - 90;
            //console.log("iframe:"+iframe.height);
        }
    }
}
function breadcrumb(m, c, a) {
    $.ajax({
        type: "POST", //提交方式
        url: "/admin/base/breadcrumb/?model=" + m + "&controller=" + c + "&action=" + a,
        success: function (result) {//返回数据根据结果进行相应的处理
            $("#breadcrumb").html(result);
        }
    });
}

/*菜单切换*/
function urlchange() {
    var url = window.location;
    $('ul.nav a').filter(function () {
        return this.href == url || url.href.indexOf(this.href) == 0;
    }).addClass('active').parent().parent().addClass('in').parent();

}

/*获取皮肤cookie*/
function getskincookie() {
    var v = getcookie("tickyskin");
    var hrefStr = $("#skin").attr("href");
    if (v == null || v == "") {
        v = "default";
    }
    debug('当前皮肤:' + v);
    if (hrefStr != undefined) {
        var hrefRes = hrefStr.substring(0, hrefStr.lastIndexOf('skin/')) + 'skin/' + v + '/skin.css';
        $("#skin").attr("href", hrefRes);
    }
}
/*获取站点cookie*/
function getsitecookie() {
    var siteid = getcookie("tickysite");
    $("#ticky-site ul li a").each(function () {
        var v = $(this).attr("data-val");
        if (v == siteid) {
            var title = $(this).html() + '<i class="fa fa-caret-down"></i>';
            $('#ticky-skin-site').html(title);
        }
    });
}
/*退出*/
function logout() {
    layer.confirm('确认要退出吗？', function (index) {
        $.post("/admin/logout", function (data, status) {
            window.location.href = "/admin/login";
        });
    });
}
/*切换语言*/
function langchange() {
    $("#ticky-lang ul li a").click(function () {
        var data = {'lang': $(this).attr("data-val")};
        debug(data);
        $.get("/admin/base/lang/", data, function () {
            location.reload();
        });
    });
}